﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;

namespace Json
{
  public static class JsonExtensions
  {
    public static bool HasAttribute(this IJsonWrapper wrapper, Type type)
    {
      if (!wrapper.IsNative)
        return false;

      Type nativeType = wrapper.GetValueType();

      return nativeType.IsDefined(type, true);
    }

    public static object GetAttributePropertyValue(this IJsonWrapper wrapper, Type type, string PropertyName)
    {
      if (!wrapper.IsNative)
        return false;

      Type nativeType = wrapper.GetValueType();

      var attribute = nativeType.GetCustomAttributes(type, true).FirstOrDefault();
      
      if (attribute == null)
      {
        throw new JsonException("Can't find atrribute");
      }

      return attribute.GetType().GetProperty(PropertyName).GetValue(attribute, null);
    }

  }
}
